var $;
if($==undefined) $ = function(id) {return document.getElementById(id);}
var JsSWX = function() {this.initialize.apply(this, arguments);};
JsSWX._resultHandlers = new Array();
JsSWX.resultHandler = function(mappingId, result) {
	var handler = JsSWX._resultHandlers[mappingId];
	handler.apply(this, [result]);
}
JsSWX.prototype = {
	id:"__jsswx",
	_jsswxObj:null,
	_swf:"jsswx.swf",
	_callCount:0,
	gateway:"",
	encoding:"POST",
	debug:false,
	initialize : function(id) {
		if(id) {
			this.id = id;
		}
		this._jsswxObj = this.createJsSWX();
	},
	createJsSWX : function() {
		var swftag = null;
		if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length) {
			swftag = document.createElement("EMBED");
			swftag.type = "application/x-shockwave-flash";
			swftag.src = this._swf;
			swftag.width = 1;
			swftag.height = 1;
			swftag.id = this.id;
			swftag.name = this.id;
			swftag.flashvars = this.getVariablePairs().join("&");
			this.getBody().appendChild(swftag);
			return swftag;
		} else {
			// IE PC
			var tag = "<object id=\"" + this.id + "\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"1\" height=\"1\">";
			var params = [
					{name:"movie", value:this._swf},
					{name:"src", value:this._swf},
					{name:"allowScriptAccess", value:"sameDomain"},
					{name:"flashvars", value:this.getVariablePairs().join("&")}
					];
			for(var i=0; i < params.length; i++) {
				tag += "<param name=\"" + params[i].name + "\" value=\"" + params[i].value + "\" />";
			}
			tag += "</object>";
			this.getBody().innerHTML += tag;
			return $( this.id );
		}
	},
	getVariablePairs : function() {
		return ["debug=true"];
	},
	getBody : function() {
		var body = null;
		if(window.document.body) {
			body = window.document.body;
		} else if(window.document.getElementsByTagName("BODY")[0]){
			body = window.document.getElementsByTagName("BODY")[0];
		}
		return body;
	},
	call : function(parameters) {
		this.setGateway( this.gateway );
		this.prepareResultHandlerMapping(parameters);
		this._jsswxObj.call(parameters);
	},
	prepareResultHandlerMapping : function(p) {
		if(p.result) {
			var callId = JsSWX._resultHandlers.length;
			var rh = p.result;
			var myJsSwx = this;
			JsSWX._resultHandlers.push(function(result) {
				// result handler should be [listenerObj, handlerMethod]
				var listener = rh[0];
				var handler = rh[1];
				handler.apply(listener, [result]);
			});
			p.result = "" + callId;
		}
	},
	setGateway : function(gateway) {
		this._jsswxObj.setGateway(gateway);
	},
	setDebug : function(debug) {
		this._jsswxObj.setDebug(debug);
	},
	setEncoding : function(encoding) {
		this._jsswxObj.setEncoding(encoding);
	}
}